from xml.etree.ElementTree import ElementTree,Element
from xml.dom.minidom import parse
import uiautomator2 as u2
import time

u2.connect('http://127.0.0.1:5038')#d = u2.connect_usb('74709ade')
#d=u2.connect('192.168.43.44')
#print(d.info)#screen=d.info#获取设备的信息#if screen["screenOn"] == False: #屏幕状态

#d.screen_on()#唤醒屏幕
def wait_activity(self, activity, timeout, interval=1):
    """Wait for an activity: block until target activity presents
    or time out.
    
    This is an Android-only method.

    :Agrs:
     - activity - target activity
     - timeout - max wait time, in seconds
     - interval - sleep interval between retries, in seconds
    """
    try:
        WebDriverWait(self, timeout, interval).until(
            lambda d: d.current_activity == activity)
        return True
    except TimeoutException:
        return False



packedgename='com.moocxuetang'#打开应用
#if d.app_info(packedgename)['label'] == '军职在线':
#    d.press('home')

#time.sleep(8)
#d.app_start(packedgename)
sess = d.session(packedgename)
print(sess)
time.sleep(8)
#fw=open('2.xml','w')
#f.seek(0)#将文件的position定位到0,否则无法清除文件内的内容，默认的position为文件的末尾
#f.truncate()#清除文件的内容  truncate：截断
#xml=d.dump_hierarchy()#获取xml树形格式数据
#fw.write(xml)#xml变量目前为字符串格式，将其写入到文件中
#tree=ElementTree()#获得xml格式的变量
#tree=parse('2.xml')读取保存的xml
#对xml中的特征提取
#print(type(tree))
#tree.write('2.xml',encoding="utf-8",xml_declaration=False)#可对xml修改后保存

if d(text='分享').exists and d(resourceId='com.moocxuetang:id/iv_today_read').exists:
    d(resourceId='com.moocxuetang:id/iv_today_read').click()
#print(wait_activity(‘我的’,5,2))
time.sleep(5)
while True:
    if d(text='学习室').exists and d(text='我的').exists:
        break;
    d.press('back')
    time.sleep(1)

#d.implicitly_wait(5)
if d(text='我的').wait(timeout=15.0):
    print('我的')
    d(text='我的').click()

#d.implicitly_wait(2)
    if d(text='打卡').wait(timeout=15.0):
        print('打卡')    
        d(text='打卡').click()
        time.sleep(3)
        #if d(resourceId='com.moocxuetang:id/tv_point_sign' ).exists:
        if d(text='点击\n打卡' ).exists:
            d.implicitly_wait(5)
            d(resourceId='com.moocxuetang:id/tv_point_sign' ).click()
            if d(text='确定' ).wait(timeout=10):
                d(text='确定' ).click()
                time.sleep(1)
        #d(resourceId='com.moocxuetang:id/iv_member_close' ).click()
            d.press('back')
        else:
            print('已经打卡')
    else:
        print('程序错误，未找到打卡选项')
#d(resourceId='com.moocxuetang:id/tv_public_left' ).click()
time.sleep(1)
#print(d(text='学习室').exists)
#print(d(text='打卡').exists)
while True:
    if d(text='学习室').exists and d(text='打卡').exists:
        break;
    d.press('back')
    time.sleep(1)
print('学习室')
#print(d(text='打卡').exists)
d.implicitly_wait(10)
d(text='学习室').click()
time.sleep(2)
d.implicitly_wait(10)
d(text='今日学习').click()
d.implicitly_wait(2)

d(text='订阅').click()
fenx=0

for i in range(1,45):
    time.sleep(2)
    if d(text='学习 32 分').exists:
        print('学习 32 分')
        break
    d.implicitly_wait(10)
    d(resourceId='com.moocxuetang:id/tv_title_item_study' ).click()
    time.sleep(10)
    if d(text='文章').exists or d(text='期刊').exists:
        #d.implicitly_wait(50)

        if d(text="学习资源奖励积分").wait(timeout=120): 
            # d(text="学习资源奖励积分" ).click()
            print(i)
            print('阅读得分')
        time.sleep(2)    
    #d(resourceId='com.moocxuetang:id/tv_back' ).click()
        if d(resourceId='com.moocxuetang:id/tv_track_one').exists:
            print('可以分享')
            if fenx < 6:
                fenx+=1
                d(resourceId='com.moocxuetang:id/tv_track_one').click()
                time.sleep(2)
                d(text='分享').click()
                time.sleep(2)
                d(text='朋友圈').click()
                time.sleep(2)

                d.press('back')
                time.sleep(3)
#            if d(text='文章').exists or d(text='期刊').exists:
#                print('没有退出键')
#            else:
#                 d(text='退出').click()
            
    while True:
        time.sleep(1)
        if d(text='订阅').exists:
             break    
        d.press('back')
                


